package org.boulderfoodrescue.webapp.client.header;

import org.boulderfoodrescue.webapp.client.managers.LoginInfoManager;
import org.boulderfoodrescue.webapp.shared.LoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;

public class HeaderView extends Composite{

	private static HeaderPanelUiBinder uiBinder = GWT
			.create(HeaderPanelUiBinder.class);

	interface HeaderPanelUiBinder extends UiBinder<Widget, HeaderView> {
	}

	@UiField
	Anchor sign_in;
	@UiField
	Anchor sign_out;  
	@UiField
	FlowPanel sign_out_section;
	@UiField 
	Hyperlink profile_name;

	public HeaderView() {
		initWidget(uiBinder.createAndBindUi(this));
		profile_name.setTargetHistoryToken("options");
		setLoginButtons();
	}
	public void setLoginButtons(){
		LoginInfo loginInfo = LoginInfoManager.getLoginInfo();
		if(loginInfo.isLoggedIn()) {
			sign_in.setVisible(false);
			sign_out_section.setVisible(true);
			sign_out.setText("Sign out");
			profile_name.setText(loginInfo.getEmailAddress());
			sign_out.setHref(loginInfo.getLogoutUrl());
		} else {
			sign_in.setHref(loginInfo.getLoginUrl());
			sign_in.setVisible(true);
			sign_out_section.setVisible(false);
		}
	}
	public Anchor getSign_in() {
		return sign_in;
	}
	public Anchor getSign_out() {
		return sign_out;
	}
}
